import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/product_userhourcost_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/product_userhourcost.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/utils/utils.dart';
import '../../../generated/l10n.dart';

class ProductUserhourcostEdit extends StatefulWidget {
  ProductUserhourcostEdit({this.productUserhourcost});

  final ProductUserhourcost? productUserhourcost;

  @override
  _ProductUserhourcostEditState createState() =>
      _ProductUserhourcostEditState();
}

class _ProductUserhourcostEditState extends State<ProductUserhourcostEdit> {
  late ProductUserhourcost productUserhourcost;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;

  //车间名称
  List<SelectOptionVO> workshopIdList = [];
  //工厂名称
  List<SelectOptionVO> factoryIdList = [];
  //设备工位
  List<SelectOptionVO> stationIdList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.productUserhourcost == null;
    this.productUserhourcost =
        widget.productUserhourcost ?? ProductUserhourcost();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    factoryIdList = await DynamicDataApi.getModelSelect(
        model: "hr.department",
        name: "complete_name",
        key: 'parent_id',
        val: null);

    if (productUserhourcost.workshopId != null) {
      workshopIdList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "hr.department",
          name: "complete_name",
          key: 'parent_id',
          val: int.parse(this.productUserhourcost.factoryId!));
    }
    if (productUserhourcost.stationId != null) {
      stationIdList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "base.productionstation",
          name: "stationname",
          key: 'workshop_id',
          val: int.parse(this.productUserhourcost.workshopId!));
    }
    if(productUserhourcost.productdate == null){
      productUserhourcost.productdate = Utils.getDate();
    }
    //activeList=[SelectOptionVO(value:'0',label: "禁用"),SelectOptionVO(value:'1',label: "启用")];
    setState(() {});
  }

  _a() {
    FormState form = formKey.currentState!;
    if (!form.validate()) {
      return;
    }
    form.save();
    ProductUserhourcostOdooApi.abc(productUserhourcost)
        .then((ResponseBodyApi res) {
      print(res.data);
      formKey.currentState!.save();

      productUserhourcost.reportyield = res.data[0][1].toString();
      productUserhourcost.hourcost = res.data[1][1].toString();
      //
      setState(() {});
    });
  }

  _b() {
    FormState form = formKey.currentState!;
    if (!form.validate()) {
      return;
    }
    form.save();
    ProductUserhourcostOdooApi.def(productUserhourcost)
        .then((ResponseBodyApi res) {
      print(res.data);
      formKey.currentState!.save();

      productUserhourcost.mouldcount = res.data[1][1].toString();
      productUserhourcost.mouldcost = res.data[0][1].toString();
      //
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          this.isAdd!
              ? CrySelect(
                  label: '工厂名称',
                  dataList: factoryIdList,
                  value: productUserhourcost.factoryId,
                  onSaved: (v) => {productUserhourcost.factoryId = v},
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                  onChange: (v) async {
                    if (productUserhourcost.factoryId != v) {
                      formKey.currentState!.save();
                      productUserhourcost.factoryId = v;
                      productUserhourcost.workshopId = null;
                      stationIdList = [];
                      try {
                        workshopIdList = await DynamicDataApi.getModelSelect(
                            active: false,
                            model: "hr.department",
                            name: "complete_name",
                            key: 'parent_id',
                            val: int.parse(v));
                      } catch (e) {
                        workshopIdList = [];
                      }
                      setState(() {});
                    }
                  },
                )
              : CryInput(
                  label: '工厂名称',
                  enable: false,
                  value: productUserhourcost.factoryName,
                  onSaved: (v) {
                    productUserhourcost.factoryName = v;
                  },
                ),
          this.isAdd!
              ? CrySelect(
                  label: '车间名称',
                  //  width: 280,
                  dataList: workshopIdList,
                  value: productUserhourcost.workshopId,
                  onSaved: (v) => {productUserhourcost.workshopId = v},
                  onChange: (v) async {
                    if (productUserhourcost.workshopId != v) {
                      formKey.currentState!.save();
                      productUserhourcost.workshopId = v;
                      productUserhourcost.stationId = null;
                      try {
                        stationIdList = await DynamicDataApi.getModelSelect(
                            active: false,
                            model: "base.productionstation",
                            name: "stationname",
                            key: 'workshop_id',
                            val: int.parse(v));
                      } catch (e) {
                        stationIdList = [];
                      }
                      setState(() {});
                    }
                  },
                )
              : CryInput(
                  label: '车间名称',
                  enable: false,
                  value: productUserhourcost.workshopName,
                  onSaved: (v) {
                    productUserhourcost.workshopName = v;
                  },
                ),
          this.isAdd!
              ? CrySelect(
                  label: '设备工位',
                  //width: 280,
                  dataList: stationIdList,
                  value: productUserhourcost.stationId,
                  onSaved: (v) => {productUserhourcost.stationId = v},
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                )
              : CryInput(
                  label: '设备工位',
                  enable: false,
                  value: productUserhourcost.stationName,
                  onSaved: (v) {
                    productUserhourcost.stationName = v;
                  },
                ),
          this.isAdd!
              ? CrySelectDate(
                  context,
                  label: '生产日期',
                  value: productUserhourcost.productdate,
                  onSaved: (v) {
                    productUserhourcost.productdate = v;
                  },
                  // validator: (v) {
                  //   return v!.isEmpty ? S.of(context).required : null;
                  // },
                )
              : CryInput(
                  label: '生产日期',
                  enable: false,
                  value: productUserhourcost.productdate,
                  onSaved: (v) {
                    productUserhourcost.productdate = v;
                  },
                ),
          Row(
            children: [
              Expanded(
                  flex: 7,
                  child: CryInput(
                    // width: 500,
                    label: '报工产量',
                    value: productUserhourcost.reportyield,
                    onSaved: (v) {
                      productUserhourcost.reportyield = v;
                    },
                  )),
              Expanded(
                  flex: 3,
                  child: CryButton(
                    label: '获取数据',
                    onPressed: () => _a(),
                  ))
            ],
          ),
          CryInput(
            label: '工时费用',
            value: productUserhourcost.hourcost,
            onSaved: (v) {
              productUserhourcost.hourcost = v;
            },
          ),
          Row(
            children: [
              Expanded(
                flex: 7,
                child: CryInput(
                  // width: 500,
                  label: '模具切换次数',
                  value: productUserhourcost.mouldcount,
                  onSaved: (v) {
                    productUserhourcost.mouldcount = v;
                  },
                ),
              ),
              Expanded(
                flex: 3,
                child: CryButton(
                  label: '获取数据',
                  onPressed: () => _b(),
                ),
              )
            ],
          ),
          CryInput(
            label: '模具切换费用',
            value: productUserhourcost.mouldcost,
            onSaved: (v) {
              productUserhourcost.mouldcost = v;
            },
          ),
          CryInput(
            label: '备注',
            value: productUserhourcost.remark,
            onSaved: (v) {
              productUserhourcost.remark = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            ProductUserhourcostOdooApi.saveOrUpdate(
                    data:productUserhourcost.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
